package com.jf.springmvc.interceptor;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:22954
 * Date:2024-11-04
 * Time:17:52
 */
@Component
public class MyInterceptor1 implements HandlerInterceptor {
    /*
    拦截器----拦截请求方法
        preHandle切片于方法执行前  返回值为true-允许放行,false-拒绝放行,后续不执行
        postHandle切片于方法执行后
        afterCompletion切片于所有执行后  若preHandle拒绝放行,该切片不执行
    多级拦截器
        默认按配置顺序设置优先级,配置顺序越高,优先级越高
        正常执行顺序:
            preHandle1 -> preHandel2 -> method -> postHandle2 -> postHandle1 -> afterHandle2 -> afterHandle1 -> return
        高优先级拒绝放行
            preHandle1 -> return
        低优先级拒绝放行
            preHandle1 -> preHandle2 -> afterHandle1
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("preHandle1");
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("postHandle1");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("afterCompletion1");
    }
}
